package com.chain.BrotherExchange.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.model.TethNumberModel;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;

import butterknife.Bind;

/**
 * 锁仓
 */
public class TradingQualificationActivity extends AbsNetworkStateActivity {


    @Bind(R.id.release_text) TextView releaseText;
//    @Bind(R.id.conversion_text) TextView conversionText;
    @Bind(R.id.apply_node_button) Button applyNodeButton;
    @Bind(R.id.locked_position_button) Button lockedPositionButton;
    @Bind(R.id.apply_unlocked_button) Button applyUnlockedButton;
//    @Bind(R.id.unlocked_list_button) Button unlockedListButton;
    @Bind(R.id.locked_position_number_text) TextView lockedPositionNumberText;
//    @Bind(R.id.unlock_position_number_text) TextView unlockPositionNumberText;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, TradingQualificationActivity.class));
    }

    @Override
    public int getTitleLayoutRes() {
        return R.layout.layout_default_title_bar_action;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_trading_qualification;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_assets_manager, R.string.details, v ->
                LockedPositionActivity.launch(v.getContext()));

        // 锁仓数据
        initLockedData();

        // 申请节点
        applyNodeButton.setOnClickListener(v ->
                showAlert(getString(R.string.determine_application_node), (dialog, which) -> applyNode())
        );

        // 锁仓
        lockedPositionButton.setOnClickListener(v ->
            showAlert(getString(R.string.determine_activation), (dialog, which) -> locked())
        );

        // 申请解仓
        applyUnlockedButton.setOnClickListener(v ->
            showAlert(getString(R.string.confirm_exit), (dialog, which) -> applyUnlocked())
        );

        // 解仓列表
//        unlockedListButton.setOnClickListener(v ->
//                UnlockedPositionListActivity.launch(v.getContext()));
    }

    private void applyUnlocked() {
        Androids.showProgressMessage(this, R.string.apply_cancellation_ing);
        String token = Androids.LocalConfig.getToken();
        int uid = Androids.LocalConfig.getUid();

        DataRepository.get().applyUnlock(uid, token, 1, result -> {
            Androids.dismissProgressMessage();
            Androids.makeText(result.getMsg());
        });
    }

    private void locked() {
        String token = Androids.LocalConfig.getToken();
        int uid = Androids.LocalConfig.getUid();
        DataRepository.get().lockToken(uid, token, 1, result -> {
            Androids.makeText(result.getMsg());
        });
    }

    private void applyNode() {
        Androids.showProgressMessage(this, R.string.application_node_ing);
        String token = Androids.LocalConfig.getToken();
        int uid = Androids.LocalConfig.getUid();

        DataRepository.get().applyNode(uid, token, result -> {
            Androids.dismissProgressMessage();
            Androids.makeText(result.getMsg());
        });
    }

    private void initLockedData() {
        loadingNetwork();
        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();

        DataRepository.get().getTokenTethNumber(uid, token, result -> {
            if (result != null && result.isSucceed()) {
                successNetwork();
                TethNumberModel.DataBean data = result.getData();
                lockedPositionNumberText.setText(data.getUsdt_lock_num());
//                unlockPositionNumberText.setText(data.getUsdt_unlock_num());
                releaseText.setText(data.getTeth_release_num());
//                conversionText.setText("1 USDT = " + data.getRate() + " TETH");
            } else if (result != null) {
                errorNetwork(result.getMsg());
            }
        });
    }

    private void showAlert(String message, Dialog.OnClickListener clickListener){
        new AlertDialog.Builder(this)
                .setTitle(R.string.dialog_prompt)
                .setMessage(message)
                .setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss())
                .setPositiveButton(R.string.sure, clickListener)
                .show();
    }

    @Override
    public void onAnewRequest() {
        initLockedData();
    }

}
